Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This fixes an unwrapped ArrayIndexOutOfBoundException in src/org/joni/ScannerSupport.
In the Lexar class, it uses the
Encoding.length()
method to retrieve the encoding characters length. According to https://github.com/jruby/jcodings/blob/master/src/org/jcodings/specific/UTF8Encoding.java, thelength()
method could returnCHAR_INVALID
(equals to -1) if the given character is invalid. The logic in Lexer does not check this return value and use it directly to change the index p. This could result in negative p value and affect future code which use p as array index like the code in https://github.com/jruby/joni/blob/master/src/org/joni/ScannerSupport.java#L136. As a result, an unexpected ArrayIndexOutOfBoundException is thrown.This PR fixes the bug by adding a conditional check for the return result of the
Encoding.length()
method before using it. An IllegalArgumentException is thrown to indicate there is invalid character in the provided regex which is more "informative" for the user instead of the general ArrayIndexOutOfBoundException.We found this bug using fuzzing by way of OSS-Fuzz, where we recently integrated joni (google/oss-fuzz#10680). OSS-Fuzz is a free service run by Google for fuzzing important open source software. If you'd like to know more about this then I'm happy to go into detail and also set up things so you can receive emails and detailed reports when bugs are found.